package com.kizuki.algorithm.leetcode;

/**
 * 给你单链表的头指针 head 和两个整数 left 和 right ，其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点，返回 反转后的链表 。
 * <p>
 * https://leetcode-cn.com/problems/reverse-linked-list-ii/
 */
public class 反转链表II {

    class Solution {
        public ListNode reverseBetween(ListNode head, int left, int right) {
            ListNode dHead = new ListNode(0);
            dHead.next = head;
            ListNode p = dHead;
            ListNode q = head;
            for (int i = 1; i < left; i++) {
                p = p.next;
                q = q.next;
            }
            ListNode move;
            for (int i = 0; i < right - left; i++) {
                move = q.next;
                q.next = q.next.next;
                move.next = p.next;
                p.next = move;
            }
            return dHead.next;
        }
    }

}
